<template>
  <div class="margin_30">
    <el-card v-if="current" shadow="never" :body-style="{padding:'20px'}" style="margin-bottom:30px">
      <div slot="header">
        <span>条件查询</span>
      </div>
      <el-form ref="form" :model="productform" label-width="80px" size="mini" :inline="true">
        <el-row :gutter="20">
          <el-col :span="4" :offset="0">
            <el-form-item>
              <el-input v-model="productform.name" placeholder="商品名称/模糊查询" />
            </el-form-item>
          </el-col>
          <el-col :span="4" :offset="0">
            <el-form-item>
              <el-input v-model="productform.productSn" placeholder="商品货号" />
            </el-form-item>
          </el-col>
          <el-col :span="4" :offset="0">
            <el-form-item>
              <el-select v-model="productform.brandId" placeholder="品牌" clearable filterable>
                <el-option
                  v-for="item in brandlist"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- publishStatus (integer, optional): 上架状态，0->下架；1->上架  -->
          <el-col :span="4" :offset="0">
            <el-form-item>
              <el-select v-model="productform.publishStatus" placeholder="上架状态" clearable filterable>
                <el-option
                  label="下架"
                  :value="0"
                />
                <el-option
                  label="上架"
                  :value="1"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4" :offset="0">
            <el-form-item>
              <!-- verifyStatus (integer, optional): 审核状态：0->未审核；1->审核通过 -->
              <el-select v-model="productform.verifyStatus" placeholder="审核状态" clearable filterable>
                <el-option
                  label="未审核"
                  :value="0"
                />
                <el-option
                  label="审核通过"
                  :value="1"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4" :offset="0">
            <el-form-item>
              <el-button @click="onSubmit">重置</el-button>
              <el-button type="primary" @click="searchGoods">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card shadow="never" :body-style="{ padding: '20px' }">
      <div slot="header">
        <span v-if="current">商品列表</span>

        <div v-else>
          <el-button type="primary" size="mini" @click="add">新增</el-button>
          <el-button type="primary" size="mini" @click="excel">导出商品列表excel文件</el-button>
        </div>
      </div>
      <!-- card body -->
      <el-table :data="list" border stripe height="550">
        <el-table-column
          label="序号"
          width="50"
          type="index"
          fixed="left"
        />
        <el-table-column
          v-if="current"
          prop=""
          label="选择"
          width="80"
        >
          <template v-slot="scope">
            <el-radio v-model="radio" :label="scope.row.id" @change="showDialog(scope.row)"><span /></el-radio>
          </template>
        </el-table-column>
        <el-table-column
          prop="pic"
          label="商品图片"
          align="center"
          width="200"
        >
          <template v-slot="scope">
            <img style="width:100px;height:100px" :src="scope.row.pic" alt="">
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="商品名称"
          align="center"
          width="250"
        />
        <el-table-column
          prop="price"
          label="商品价格"
          width="150"
          align="center"
        >
          <template v-slot="scope">
            <div>
              原价:{{ scope.row.originalPrice }}
            </div>
            <div>
              现价:{{ scope.row.price }}
            </div>
          </template>
        </el-table-column>

        <el-table-column
          prop="productCategoryName"
          label="商品类别"
          width="180"
          align="center"
        />
        <el-table-column
          v-if="!current"
          label="标签"
          align="center"
          width="150"
        >
          <template v-slot="scope">
            <!-- newStatus (integer, optional): 新品状态:0->不是新品；1->新品 , -->
            <div>
              最新：<el-switch v-model="scope.row.newStatus" :active-value="1" :inactive-value="0" @change="isNewStatus(scope.row.id, scope.row.newStatus)" />
            </div>
            <!-- recommendStatus (integer, optional): 推荐状态；0->不推荐；1->推荐 , -->
            <div>
              推荐：<el-switch v-model="scope.row.recommendStatus" :active-value="1" :inactive-value="0" @change="isRecommendStatus(scope.row.id, scope.row.recommendStatus)" />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          v-if="!current"
          label="标签2"
          align="center"
          width="150"
        >
          <template v-slot="scope">
            <!-- publishStatus (integer, optional): 上架状态：0->下架；1->上架 , -->
            <div>
              发布：<el-switch v-model="scope.row.publishStatus" :active-value="1" :inactive-value="0" @change="isPublishStatus(scope.row.id, scope.row.publishStatus)" />
            </div>
            <!-- verifyStatus (integer, optional): 审核状态：0->未审核；1->审核通过 , -->
            <div>
              审核：<el-switch v-model="scope.row.verifyStatus" :active-value="1" :inactive-value="0" @change="isVerifyStatus(scope.row.id, scope.row.verifyStatus)" />
            </div>
          </template>
        </el-table-column>

        <el-table-column
          v-if="!current"
          prop=""
          label="sku"
          width="120"
          align="center"
        >
          <template slot-scope="scope">
            <el-button type="text" size="default" @click="editSku(scope.row.id)">编辑sku</el-button>
          </template>
        </el-table-column>
        <el-table-column
          prop="weight"
          label="重量"
          width="90"
          align="center"
        />
        <el-table-column
          v-if="!current"
          prop="sort"
          label="排序"
          width="90"
          align="center"
        />

        <el-table-column v-if="!current" label="操作" align="center" fixed="right" width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="goEdit(scope.row.id)">编辑</el-button>
            <el-button style="color:red;" size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="paginationParams.start"
        :page-sizes="paginationParams.pageSize"
        :page-size="paginationParams.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <!-- 点击单选框出现弹窗 -->
    <el-dialog
      title="编辑限时活动"
      :visible.sync="isShowDialog"
      width="50%"
      append-to-body
    >
      <el-form ref="form" :model="form" label-width="100px" :inline="false" size="small">
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item label="商品原价">
              {{ form.price }}
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="商品名称">
              {{ form.name }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item label="促销价格">
              <el-input-number v-model="form.promotionPrice" label="描述文字" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="排序">
              <el-input-number v-model="form.sort" label="描述文字" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" style="width:85%">
              <el-date-picker
                v-model="form.promotionStartTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="开始时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" style="width:85%">
              <el-date-picker
                v-model="form.promotionEndTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="结束时间"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item label="活动限购数量">
              <el-input-number v-model="form.promotionPrice" label="" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="商品图片">
              <img style="width:150px;height:150px" :src="form.pic" alt="">
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8" :offset="16">
            <el-button @click="cancle">取消</el-button>
            <el-button type="primary" @click="save">确定</el-button>
          </el-col>
        </el-row>

      </el-form>

    </el-dialog>

  </div>
</template>

<script>
import mix from '@/mixins'
import { addRecommend } from '@/api/market/activity'

export default {
  mixins: [mix],
  props: ['list', 'total', 'productform', 'brandlist', 'current'],
  data() {
    return {
      isShowDialog: false,
      radio: '',
      form: {
        'brandName': '',
        'createTime': '',
        'modifyTime': '',
        'name': '',
        'pic': '',
        'price': '',
        'productCategoryName': '',
        'productId': '',
        'productSn': '',
        'promotionEndTime': '',
        'promotionPerLimit': '',
        'promotionPrice': '',
        'promotionStartTime': '',
        'publishStatus': '',
        'recommendId': '',
        'sort': '',
        'type': ''
      }
    }
  },

  mounted() {

  },

  methods: {
    // 编辑sku
    editSku(id) {
      this.$emit('editSku', id)
    },
    // 编辑
    goEdit(id) {
      this.$router.push(`/product/list/uptGoodsList/${id}`)
    },
    cancle() {
      this.isShowDialog = !this.isShowDialog
    },
    // 编辑限时活动保存
    save() {
      this.form.productId = this.form.id
      addRecommend(this.form)
        .then(res => {
          console.log(res)
          this.isShowDialog = !this.isShowDialog
        })
    },
    // 点击单选框显示的弹窗
    showDialog(e) {
      this.isShowDialog = !this.isShowDialog
      this.form = e
    },
    // 每页多少条改变时触发（main.js中切换页码中文）
    handleSizeChange(e) {
      console.log(e)
      this.$emit('uptlimit', e)
    },
    // 当前页码发生改变触发
    handleCurrentChange(e) {
      console.log(e)
      this.$emit('uptstart', e)
    },
    // 重置
    onSubmit() {
      this.$emit('resetgoods', this.productform)
    },
    // 条件搜索
    searchGoods() {
      this.$emit('searchgoods', this.productform)
    },
    // 最新
    isNewStatus(id, newStatus) {
      this.$emit('isNewStatus', { productId: id, status: newStatus })
    },
    // 推荐
    isRecommendStatus(id, recommendStatus) {
      this.$emit('isRecommendStatus', { productId: id, status: recommendStatus })
    },
    // 发布
    isPublishStatus(id, publishStatus) {
      this.$emit('isPublishStatus', { productId: id, status: publishStatus })
    },
    // 审核
    isVerifyStatus(id, verifyStatus) {
      this.$emit('isVerifyStatus', { productId: id, status: verifyStatus })
    },
    // 跳转新增页面
    add() {
      this.$router.push({ name: 'AddGoods' })
    },
    // 导出excel文件
    excel() {
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['商品名称', '商品价格']
        const filterVal = ['name', 'price']
        const list = this.list
        const data = this.formatJson(filterVal, list)
        excel.export_json_to_excel({
          header: tHeader, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 非必填
          autoWidth: true, // 非必填
          bookType: 'xlsx' // 非必填
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
